<?php

/**
 * This is the model class for table "backup_locations".
 *
 * The followings are the available columns in table 'backup_locations':
 * @property integer $id_backup_locations
 * @property string $name
 * @property string $protocol
 * @property string $server
 * @property string $username
 * @property string $password
 * @property integer $port
 * @property string $directory
 */
class BackupLocations extends CActiveRecord
{
	const PROTOCOL_HOMEDIR = 'homedir';
	const PROTOCOL_SCP = 'scp';
	const PROTOCOL_FTP = 'ftp';
	const PROTOCOL_PASSIVEFTP = 'passiveftp';
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return BackupLocations the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'backup_locations';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, protocol', 'required'),
//			array('server, username, password, port, directory', 'safe', 'on'=>'homedir'),
			array('server, username, password, port, directory', 'required', 'on'=>'not_homedir'),
			
			array('port', 'numerical', 'integerOnly'=>true),
			array('name, protocol, server, username, password, directory', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_backup_locations, name, protocol, server, username, password, port, directory', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_backup_locations' => 'Id Backup Locations',
			'name' => 'Name',
			'protocol' => 'Protocol',
			'server' => 'Server',
			'username' => 'Username',
			'password' => 'Password',
			'port' => 'Port',
			'directory' => 'Directory',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_backup_locations',$this->id_backup_locations);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('protocol',$this->protocol,true);
		$criteria->compare('server',$this->server,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('port',$this->port);
		$criteria->compare('directory',$this->directory,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort' => array(
				'defaultOrder'=>'name ASC',
				'attributes' => array(
					'name_sort' => array(
						'asc' => 't.name',
						'desc' => 't.name DESC',
					),
					'protocol_sort' => array(
						'asc' => 't.protocol',
						'desc' => 't.protocol DESC',
					),
					'server_sort' => array(
						'asc' => 't.server ',
						'desc' => 't.server DESC',
					),
					'username_sort' => array(
						'asc' => 't.username',
						'desc' => 't.username DESC',
					),
					'port_sort' => array(
						'asc' => 't.port',
						'desc' => 't.port DESC',
					),
					'directory_sort' => array(
						'asc' => 't.directory',
						'desc' => 't.directory DESC',
					),
				)
			),
			'pagination' => array(
				'pageSize'=> app()->user->getData()->getIndividualConfig()->getPagination()
			)
		));
	}
	
	public function renderListActionButtons()
	{
		return '
			<div class="btn-group">
				<button class="btn btn-info dropdown-toggle actions" data-toggle="dropdown">
					'. t('buttons', 'Actions') .'
					<span class="caret"></span>
				</button>
				<ul class="dropdown-menu">
					<li class="backuplocation_edit"><a href="javascript:void(0);"><i class="radmin-icon radmin-pencil"></i> '. t('buttons', 'Edit') .'</a></li>
					<li class="backuplocation_delete"><a href="javascript:void(0);"><i class="radmin-icon radmin-remove"></i> '. t('buttons', 'Delete') .'</a></li>
				</ul>
				
				<div class="delete_confirm_btn hide">
					<a class="btn btn-small delete_confirm"><i class="icon-ok"></i>'. t('buttons', 'Confirm Delete') .'</a>
					<a class="btn btn-small delete_cancel"><i class="icon-remove"></i>'. t('buttons', 'Cancel') .'</a>
				</div>
			</div>
		';
	}
}